package edu.njust.controller;
import edu.njust.pojo.*;
import edu.njust.service.CourseService;
import edu.njust.service.UserService;
import edu.njust.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.sql.Date;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/CourseController")
public class CourseController {

    @Autowired
    @Qualifier("CourseServiceImpl")
    private CourseService courseService;

    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    private DateUtils dateUtils;


    //----------------------学生-----------------------------------------------------------------------------------------
    // 实验课
    //学生查询已选择的实验课
    @RequestMapping("/stuQuerySelectedLesson")
    public List<CourseInfo>  showStuSelectedLesson(@RequestParam("student_id") String student_id){
        List<ReservationItem> reservationItemList = courseService.getSelectedKlassReservation(student_id);
        List<CourseInfo> courseInfoList = courseService.getCourseInfoList(reservationItemList);
        return courseInfoList;
    }

    //学生查询可选择的实验课
    @RequestMapping("/stuQueryLessonForSelect")
    public List<CourseInfo> showStuLessonForSelect(@RequestParam("student_id") String student_id){
        List<ReservationItem> reservationItemList = courseService.searchExistingKlassReservationForStudent(student_id);
        List<CourseInfo> courseInfoList = courseService.getCourseInfoList(reservationItemList);
        return courseInfoList;
    }

    //学生选择实验课参加
    @RequestMapping("/stuSelectLesson")
    public Integer studentSelectLesson(@RequestParam("lesson_id") Long reservationItem_id, @RequestParam("student_id") String student_id){
        Integer isSuccess = courseService.selectKlassReservationForStudent(reservationItem_id, student_id);
        return isSuccess;
    }

    //学生退选实验课
    @RequestMapping("/stuQuitLesson")
    public Integer studentQuitLesson(@RequestParam("lesson_id") Long reservationItem_id, @RequestParam("student_id") String student_id){
        Integer isQuit = courseService.quitKlassReservationForStudent(reservationItem_id, student_id);
        return isQuit;
    }

    // 考试
    //学生查询自己的考试
    @RequestMapping("/stuQueryExam")
    public List<CourseInfo> showStudentExam(@RequestParam("student_id") String student_id){
        List<ReservationItem> reservationItemList = courseService.searchOwnExamReservationForStudent(student_id);
        List<CourseInfo> courseInfoList = courseService.getCourseInfoList(reservationItemList);
        return courseInfoList;
    }

    // 开放实验
    //学生查看已选的开放实验
    @RequestMapping("/stuQuerySelectedLab")
    public List<CourseInfo> showStuSelectedLab(@RequestParam("/student_id") String student_id){
        List<ReservationItem> reservationItemList = courseService.getSelectedLabReservation(student_id);
        List<CourseInfo> courseInfoList = courseService.getCourseInfoList(reservationItemList);
        return courseInfoList;
    }

    //学生选择开放实验加入
    @RequestMapping("/stuSelectLab")
    public Integer stuSelectLab(@RequestParam("reservation_id") Long reservation_id,@RequestParam("student_id") String student_id){
        Integer isAdd = courseService.selectLabReservation(reservation_id, student_id);
        return isAdd;
    }

    //学生选择开放实验退出
    @RequestMapping("/stuQuitLab")
    public Integer stuQuitLab(@RequestParam("reservation_id") Long reservation_id, @RequestParam("student_id") String student_id){
        Integer isQuit = courseService.quitLabReservation(reservation_id, student_id);
        return isQuit;
    }

    // 比赛
    //学生查询自己的比赛
    @RequestMapping("/stuQueryCompetition")
    public List<CourseInfo> showStudentCompetition(@RequestParam("student_id") String student_id){
        List<ReservationItem> reservationItemList = courseService.getExistingCompetitionReservation(student_id);
        List<CourseInfo> courseInfoList = courseService.getCourseInfoList(reservationItemList);
        return courseInfoList;
    }


    //----------------------老师-----------------------------------------------------------------------------------------
    //老师查看自己的排课
    @RequestMapping("teaQueryLesson")
    public List<KlassReservation> showTeacherLesson(@RequestParam("teacher_id") String id){
        List<KlassReservation> klassReservationList = courseService.getReservationListByTeacherId(id);
        return klassReservationList;
    }

    //老师和管理员取消排课(实验和开放性实验)
    @RequestMapping("/cancelLesson")
    public Integer teacherQuitLesson(@RequestParam("lesson_id") Long reservation_id, @RequestParam("type") Integer type){
        Integer isQuit = courseService.quitReservation(reservation_id, type);
        return isQuit;
    }

    //TODO
    //通过老师id显示所有课程
    public List<Course> teacherQueryAllCourse(@RequestParam("teacher_id") String teacher_id){
        List<Course> courseList = courseService.getCourseListForTeacher(teacher_id);
        return courseList;
    }

    //老师和管理员选课返回可用机房
    @RequestMapping("/RoomForSelect")
    public List<ComputerRoom> showComputerRoomForUse(@RequestParam("date") Date date, @RequestParam("num") Integer time_num, @RequestParam("type") Integer type){
        List<ComputerRoom> computerRoomList = courseService.getComputerRoomListByType(date, time_num, type);
        return computerRoomList;
    }

    //老师和管理员选课显示该课的所有班级信息
    @RequestMapping("/classForSelect")
    public List<Klass> showClassList(@RequestParam("teacher_id") String id, @RequestParam("course_name") String name){
        List<Klass> klassList = courseService.getKlassList(id, name);
        return klassList;
    }

    //老师和管理员确认排课
    @RequestMapping("/addLesson")
    public Integer teacherAddLesson(Map<String, Object> map) throws ParseException {
        String user_id = String.valueOf(map.get("user_id"));
        String course_name = String.valueOf(map.get("course_name"));
        String scheduling_name = String.valueOf(map.get("scheduling_name"));
        Date date = dateUtils.toDate((String) map.get("date"));
        Integer time_num = Integer.valueOf((Integer) map.get("num"));
        boolean is_shared = (boolean) map.get("is_shared");
        Integer reservation_type = Integer.valueOf((Integer) map.get("type"));
        List<String> computer_room_idList = (List<String>)(map.get("computer_room_idList"));
        List<String> klass_idList = (List<String>)(map.get("klass_idList"));
        courseService.addKlassReservation(user_id, course_name, scheduling_name,
                date, time_num, is_shared, reservation_type, computer_room_idList, klass_idList);
        Integer isSuccess = 1;
        return isSuccess;
    }

    //老师和管理员安排开放性实验
    @RequestMapping("/addLab")
    public Integer addLabReservation(Map<String, Object> map) throws ParseException {
        String user_id = String.valueOf(map.get("user_id"));
        String scheduling_name = String.valueOf(map.get("scheduling_name"));
        Date begin_time = dateUtils.toDate((String) map.get("begin_time"));
        Date end_time = dateUtils.toDate((String) map.get("end_time"));
        Integer plan_num = Integer.parseInt((String)map.get("plan_num"));
        courseService.addLabReservation(user_id, scheduling_name, begin_time, end_time, plan_num);
        Integer isSuccess = 1;
        return isSuccess;
    }

    //----------------------管理员-----------------------------------------------------------------------------------------
    //管理员添加比赛
//    public Integer addCompetition(Map<String, Object> map) throws ParseException {
//        String user_id = String.valueOf(map.get("user_id"));
//        String Scheduling_name = String.valueOf(map.get("Scheduling_name"));
//        Date date = dateUtils.toDate((String) map.get("date"));
//        Time begin_time =
//        Time end_time = ;
//        List<String> computer_room_id = (List<String>) (map.get("computer_room_id"));
//        List<String> student_id = (List<String>) (map.get("student_id"));
//        Integer isAdd = courseService.addCompetitionReservation(user_id, Scheduling_name, date, begin_time, end_time,
//                computer_room_id, student_id);
//
//    }

    //管理员显示所有比赛

    //TODO
    //管理员按关键字查询课程course（考试）

    //管理员确认安排考试
    @RequestMapping("/adminAddExam")
    public String addExam(Map<String, Object> map) throws ParseException {
        String admin_id = String.valueOf(map.get("admin_id"));
        String course_name = String.valueOf(map.get("course_name"));
        String scheduling_name = String.valueOf(map.get("scheduling_name"));
        Date date = dateUtils.toDate((String) map.get("date"));
        Integer time_num = Integer.valueOf((Integer) map.get("num"));
        Integer reservation_type = Integer.valueOf((Integer) map.get("type"));
        List<String> computer_room_id = (List<String>)(map.get("computer_room_id"));
        courseService.addExamReservation(admin_id, course_name, scheduling_name, date , time_num, reservation_type, computer_room_id);
        return "";
    }

    //管理员通过实验名称、老师名字、日期查询所有安排
    @RequestMapping("/adminQueryLessonByInfo")
    public List<Reservation> checkScheduling(@RequestParam("scheduling_name") String scheduling_name,
                                  @RequestParam("teacher_name") String teacher_name, @RequestParam("date") Date date) throws ParseException {
        DateUtils dateUtils = new DateUtils();
        List<Reservation> reservationList =
                courseService.getReservationList(scheduling_name, teacher_name, dateUtils.toDate(date.toString()).toString());
        return reservationList;
    }

    //管理员显示所有机房安排
    @RequestMapping("/adminQueryAllRoom")
    public List<ReservationItem> getAllComputerRoom(){
        List<ReservationItem> reservationItemList = courseService.getAllRoomReservation();

        return reservationItemList;
    }

    //管理员显示所有课程实验安排
    @RequestMapping("/adminQueryAllLesson")
    public List<KlassReservation> getAllLesson(){
        List<KlassReservation> klassReservationList = courseService.getAllKlassReservation();

        return klassReservationList;

    }

    //管理员显示所有考试安排
    @RequestMapping("/adminQueryAllExam")
    public List<ExamReservation> getAllExam(){
        List<ExamReservation> examReservationList = courseService.getAllExamReservation();
        return examReservationList;
    }

    //显示所有开放性实验
    @RequestMapping("/showAllLab")
    public List<CourseInfo> getAllLabScheduling(){
        List<ReservationItem> labReservationItemList = courseService.getAllLabReservationList();
        List<CourseInfo> courseInfoList = courseService.getCourseInfoList(labReservationItemList);
        return courseInfoList;
    }

    //显示当前开放性实验
    @RequestMapping("/showNowLab")
    public List<LabReservation> showNowLabScheduling(){
        List<LabReservation> labReservationList = courseService.getNowLabReservationList();
        return labReservationList;
    }




}
